package test.src.awt_swing;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class SwingComponentDemo {
    JFrame jf = new JFrame("测试swing基本组件");

    JMenuBar jMenuBar = new JMenuBar();

    JMenu fileMenu = new JMenu("文件");
    JMenu editMenu = new JMenu("编辑");

    JMenuItem auto = new JMenuItem("自动换行");
    JMenuItem copy = new JMenuItem("复制");
    JMenuItem paste = new JMenuItem("粘贴");
    JMenu formatMenu = new JMenu("格式");

    JPopupMenu popupMenu = new JPopupMenu();
    ButtonGroup formatButtonGroup = new ButtonGroup();
    JRadioButtonMenuItem metalItem = new JRadioButtonMenuItem("Metal风格");
    JRadioButtonMenuItem nimbusItem = new JRadioButtonMenuItem("Nimbus风格");
    JRadioButtonMenuItem windowsItem = new JRadioButtonMenuItem("Windows风格");
    JRadioButtonMenuItem windowsClassicItem = new JRadioButtonMenuItem("Windows经典风格");
    JRadioButtonMenuItem motifItem = new JRadioButtonMenuItem("motif风格");

    JMenuItem comment = new JMenuItem("注释");
    JMenuItem cancelComment = new JMenuItem("取消注释");

    JTextArea textArea = new JTextArea(8, 40);

    JComboBox<String> comboBox = new JComboBox<>();

    ButtonGroup buttonGroup = new ButtonGroup();
    JRadioButton male = new JRadioButton("男", false);
    JRadioButton female = new JRadioButton("女", true);

    JCheckBox isMarried = new JCheckBox("是否已婚", true);

    JTextField textField = new JTextField(40);
    JButton ok = new JButton("确定");

    private void changeFlavor(String command) throws Exception {
        switch (command) {
            case "Metal风格" -> UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
            case "Nimbus 风格" -> UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
            case "Windows 风格" -> UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
            case "Windows 经典风格" -> UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel");
            case "Motif 风格" -> UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
        }

        //刷新组件的外观
        SwingUtilities.updateComponentTreeUI(jf.getContentPane());
        SwingUtilities.updateComponentTreeUI(jMenuBar);
        SwingUtilities.updateComponentTreeUI(popupMenu);
    }

    ActionListener actionListener = e -> {
        String s = e.getActionCommand();
        try {
            System.out.println(s);
            changeFlavor(s);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    };

    public void init() {
        formatMenu.add(comment);
        formatMenu.add(cancelComment);

        editMenu.add(auto);
        editMenu.addSeparator();
        editMenu.add(copy);
        editMenu.add(paste);
        editMenu.addSeparator();
        editMenu.add(formatMenu);

        jMenuBar.add(fileMenu);
        jMenuBar.add(editMenu);

        jf.setJMenuBar(jMenuBar);

        Box bottom = Box.createHorizontalBox();
        bottom.add(textField);
        bottom.add(ok);

        Box top = Box.createHorizontalBox();
        Box topLeft = Box.createVerticalBox();

        Box choiceBox = Box.createHorizontalBox();
        comboBox.addItem("红色");
        comboBox.addItem("绿色");
        comboBox.addItem("蓝色");
        choiceBox.add(comboBox);
        buttonGroup.add(male);
        buttonGroup.add(female);
        choiceBox.add(male);
        choiceBox.add(female);
        choiceBox.add(isMarried);

        topLeft.add(textArea);
        topLeft.add(choiceBox);

        JList<String> list = new JList<>(new String[] {"红色", "绿色", "蓝色"}) ;
        top.add(topLeft);
        top.add(list);

        jf.add(top);
        jf.add(bottom, BorderLayout.SOUTH);

        popupMenu.add(metalItem);
        popupMenu.add(nimbusItem);
        popupMenu.add(windowsItem);
        popupMenu.add(windowsClassicItem);
        popupMenu.add(motifItem);

        metalItem.addActionListener(actionListener);
        nimbusItem.addActionListener(actionListener);
        windowsClassicItem.addActionListener(actionListener);
        windowsItem.addActionListener(actionListener);
        motifItem.addActionListener(actionListener);

        formatButtonGroup.add(metalItem);
        formatButtonGroup.add(nimbusItem);
        formatButtonGroup.add(windowsItem);
        formatButtonGroup.add(windowsClassicItem);
        formatButtonGroup.add(motifItem);

        textArea.setComponentPopupMenu(popupMenu);

        jf.pack();
        jf.setVisible(true);
    }

    public static void main(String[] args) {
        new SwingComponentDemo().init();
    }
}
